﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace BaiTu.Common.Web.Helpers
{
    /// <summary>
    /// 文件映射Helper类
    /// </summary>
    public sealed class MapPathHelper : Framework.Interfaces.ISingletonDependency
    {

        private MapPathHelper() { }
        /// <summary>
        /// 将虚拟路径映射为服务器路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetPhyicalPath(string path)
        {
            if (HttpContext.Current != null)
            {
                return System.Web.Hosting.HostingEnvironment.MapPath(path);
            }
            else
            {
                ///非web引用
                path = path.Replace("~","");
                path = path.Replace("/", "\\");
                if (path.StartsWith("\\"))
                {
                    path = path.Substring(path.IndexOf('\\', 1)).TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
            }
        }
    }
}
